package bean.util;

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import bean.ColorCheckBean;
import br.com.wagnerpaz.javahook.HookManager;
import collector.listener.CollectorKeyboardListener;
import collector.listener.CollectorMouseListener;

import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;

public class WindowUtil {

	public static Robot robot = null;

	public static ArrayList<HWND> cgWins = new ArrayList<HWND>();

	public static HWND toolWin = null;

	public static HWND[] petWins = new HWND[10];

	public static void init() {
		try {
			robot = new Robot();
		} catch (Exception e) {
		}

		toolWin = User32.INSTANCE.FindWindow(null, TOOL_NAME);

		WNDENUMPROC proc = new WNDENUMPROC() {
			public boolean callback(HWND hwnd, Pointer data) {
				if (User32.INSTANCE.IsWindowVisible(hwnd)) {
					char[] buffer = new char[256];
					User32.INSTANCE.GetWindowText(hwnd, buffer, buffer.length);
					String windowTitle = String.copyValueOf(buffer).trim();

					if (windowTitle.equals(CG_NAME)) {
						cgWins.add(hwnd);
					}
				}

				return true;
			}
		};

		WNDENUMPROC proc1 = new WNDENUMPROC() {
			public boolean callback(HWND hwnd, Pointer data) {
				if (User32.INSTANCE.IsWindowVisible(hwnd)) {
					int winId = User32.INSTANCE.GetWindowLong(hwnd,
							User32.GWL_ID);
					for (int i = 0; i < TOOL_WINDOW_INDEXS.length; i++) {
						if (winId == TOOL_WINDOW_INDEXS[i]) {
							petWins[i] = hwnd;
							break;
						}
					}
				}

				return true;
			}
		};

		User32.INSTANCE.EnumWindows(proc, null);
		User32.INSTANCE.EnumChildWindows(toolWin, proc1, null);
	}

	public static void hookKeyboard() {
		HookManager.installKeyboardHook(new CollectorKeyboardListener());
	}

	public static void hookMouse(HWND window) {
		HookManager.installMouseHook(new CollectorMouseListener(window));
	}

	public static void activeWindow(HWND window) {
		HWND currentWindow = User32.INSTANCE.GetForegroundWindow();
		if (currentWindow != window) {
			User32.INSTANCE.ShowWindow(window, WinUser.SW_NORMAL);
			User32.INSTANCE.SetForegroundWindow(window);

			robot.delay(100);
		}
	}

	public static void closeWindow(HWND window) {
		User32.INSTANCE.CloseWindow(window);
		robot.delay(50);
	}

	public static RECT getWindowRect(HWND window) {
		RECT rect = new RECT();
		User32.INSTANCE.GetWindowRect(window, rect);

		return rect;
	}

	public static int[] getPetAttr(HWND window) {
		int[] ret = new int[3];
		RECT rect = getWindowRect(window);

		int w = rect.right - rect.left;
		int h = rect.bottom - rect.top;

		Rectangle rectangle = new Rectangle();
		rectangle.setBounds(rect.left, rect.top, w, h);

		BufferedImage bi = robot.createScreenCapture(rectangle);

		int cnt = 0;
		while (true) {
			int x = cnt * (NAME_RECT.width + 1) + NAME_RECT.x;
			if (x > w) {
				cnt = -1;
				break;
			}

			int pixelNum = 0;
			for (int j = NAME_RECT.y; j < NAME_RECT.y + NAME_RECT.height; j++) {
				for (int i = x; i < x + NAME_RECT.width; i++) {
					if (bi.getRGB(i, j) == -4194304) {
						pixelNum++;
					}
				}
			}

			if (pixelNum == 0) {
				break;
			} else {
				cnt++;
			}
		}

		ret[0] = cnt;

		cnt = 0;
		while (true) {
			int x = cnt * (HP_RECT.width + 1) + HP_RECT.x;
			if (x > w) {
				cnt = -1;
				break;
			}

			int id = -2;
			for (int[] matrix : NUMBER_MATRIX) {
				if (bi.getRGB(x + matrix[0], HP_RECT.y + matrix[1]) == -4194304) {
					id = matrix[2];
					break;
				}
			}

			if (id < 0) {
				break;
			} else {
				cnt++;
				ret[1] = ret[1] * 10 + id;
			}
		}

		cnt = 0;
		while (true) {
			int x = cnt * (MP_RECT.width + 1) + MP_RECT.x;
			if (x > w) {
				cnt = -1;
				break;
			}

			int id = -2;
			for (int[] matrix : NUMBER_MATRIX) {
				if (bi.getRGB(x + matrix[0], MP_RECT.y + matrix[1]) == -4194304) {
					id = matrix[2];
					break;
				}
			}

			if (id < 0) {
				break;
			} else {
				cnt++;
				ret[2] = ret[2] * 10 + id;
			}
		}

		return ret;
	}

	public static void mouseClick(HWND window, int x, int y, boolean isLeft) {
		int button = isLeft ? InputEvent.BUTTON1_MASK : InputEvent.BUTTON3_MASK;
		RECT rect = getWindowRect(window);

		robot.mouseMove(x + rect.left, y + rect.top);
		robot.delay(50);

		robot.mousePress(button);
		robot.delay(50);

		robot.mouseRelease(button);
		robot.delay(50);
	}

	public static boolean checkPixelColor(HWND window, ColorCheckBean checker) {
		RECT rect = getWindowRect(window);

		int x = checker.getX() + rect.left;
		int y = checker.getY() + rect.top;

		int rgb = robot.getPixelColor(x, y).getRGB();

		return checker.isMatch(rgb);
	}

	private static final String CG_NAME = "唯有魔力 7.0&3.7 [一线]";

	private static final String TOOL_NAME = "看血小工具 － By 第六天不良  － 人物：痛苦女王";

	private static final int[] TOOL_WINDOW_INDEXS = { 37, 38, 39, 40, 41, 42,
			51, 50, 43, 44 };

	private static final Rectangle HP_RECT = new Rectangle(29, 29, 5, 10);

	private static final Rectangle MP_RECT = new Rectangle(29, 42, 5, 10);

	private static final Rectangle NAME_RECT = new Rectangle(5, 14, 11, 11);

	private static final int[][] NUMBER_MATRIX = { { 1, 2, 1 }, { 0, 8, 2 },
			{ 1, 3, 4 }, { 4, 0, -1 }, { 2, 6, 7 }, { 3, 5, 9 }, { 4, 1, 5 },
			{ 3, 2, 6 }, { 4, 4, 0 }, { 0, 3, 8 }, { 1, 1, 3 } };

}
